//index.js
//获取应用实例
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({
  data: {
    pageIndex: 1,
    pageCount: 10,
    dataList: [],
    isEmpty: false,
    imageList: ["/images/home/jb_add.png"],
    juBaoText: "",
    juBaoType: -1,
    itemId: -1,
    isShowJuBao: true,
    isShowMoreFloatView: false,
    requestUrl:"",
    authorUserId:"",
    isShowFollowView:false,
    userDetailData:{},
    isFollow:false,
  },

  onLoad: function (options) {
    this.data.authorUserId = options.authorUserId;
    this.data.requestUrl = app.userPostListUrl;
    this.getUserData(this.data.authorUserId);
    this.getFollowData(this.data.authorUserId);

  },
  onShow(){
    this.data.pageIndex = 1;
    this.getListData();
  },
  getUserData(userId) {
    var _this = this;
    var map = {};
    map.appId = app.APP_ID;
    map.userId = userId;
    api.getRequestDataNoLoading(app.getUserDataByUserIdUrl, map, false, "GET").then(res => {
      console.log(">>>>>>>",JSON.stringify(res));
      wx.setNavigationBarTitle({
        title: res.nickname,
      })
      _this.setData({
        userDetailData:res
      });

    });
  },
  getFollowData(userId){
    var _this = this;
    var map = {};
    map.appId = app.APP_ID;
    map.userId = app.USER_ID();
    map.userId2 = userId;
    api.getRequestDataNoLoading(app.isFollowUrl, map, false, "GET").then(res => {
      _this.setData({
        isFollow:res
      });

    });
  },
  getListData() {
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.pageCount = this.data.pageCount;
    map.pageIndex = this.data.pageIndex;
    map.authorUserId = this.data.authorUserId;
    api.getRequestData(this.data.requestUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model.list;
        if (list.length == 0) {
          app.alert("没有更多数据了～");
        }
        for (var i = 0; i < list.length; i++) {
          var dict = list[i];
          var content = dict.content;
          var contents = content.split("#");
          dict.contents = contents;
          var imgs = JSON.parse(dict.imgs);
          dict.imgs = imgs;

        }
        if (_this.data.pageIndex == 1) {
          _this.data.dataList = list;
          _this.setData({
            dataList: _this.data.dataList
          });
        } else {
          // 加载更多
          _this.setData({
            dataList: _this.data.dataList.concat(list)
          });
        }
        if (_this.data.dataList.length == 0) {
          _this.setData({
            isEmpty: true
          });
        } else {
          _this.setData({
            isEmpty: false
          });
        }
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  postItemClick(e) {
    var index = e.currentTarget.dataset.key;
    var dict = this.data.dataList[index];
    var userId = dict.userId;
    var id = dict.postId;
    if (!id){
      id = dict.id;
    }
    var path = '/pages/index/detail/detail?USER_ID=' + userId + '&POST_ID=' + id;
    wx.navigateTo({
      url: path,
    })
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.getListData();

  },
  followClick(e) {
    var _this = this;
    var map = {};
    map.fanUserId = app.USER_ID();
    map.type = "5";
    if (app.USER_ID() == this.data.authorUserId) {
      app.alert("不能关注自己呀~");
      return;
    }
    map.userId = this.data.authorUserId;

    api.getRequestData(app.userFansSaveUrl, map, false, "POST").then(res => {
      if (res.data.errorCode == 0) {
        app.showToasts("关注成功~");
        app.HOMENEEDFRESH = true;
        _this.setData({
          isFollow: true
        });
        
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  canFollow(e) {
    this.setData({
      isShowFollowView:true
    });
    return;
    var _this = this;
    var index = e.currentTarget.dataset.key;
    var item = this.data.dataList[index];
    wx.showModal({
      title: '提示',
      content: '确定不在关注?',
      confirmText: '确定',
      confirmColor: '#E97D6B',
      success: function (res) {
        if (res.confirm) {
          _this.cancelFollow(item);
        } else if (res.cancel) {
        }
      }
    });
  },
  cancelFollow() {
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.userId2 = this.data.authorUserId;
    api.getRequestData(app.deleteUserFansByUserId, map, false, "GET").then(res => {
      if (res.data.errorCode == 0) {
        app.HOMENEEDFRESH = true;
        app.alert("取消关注成功~");
        _this.setData({
          isFollow:false
        });
       
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  canceFlollowClick(e){
    this.setData({
      isShowFollowView: false
    });
    var key = e.currentTarget.dataset.key;
    if (key == 1) {
      this.cancelFollow();
    }

  },
  onReachBottom: function () {
    this.data.pageIndex += 1;
    this.getListData();
  },
  juBaoMoreClick(e) {
    var key = e.currentTarget.dataset.key;
    this.data.itemId = key;
    this.setData({
      isShowMoreFloatView: true
    });
  },
  hiddenFloatView() {
    this.setData({
      isShowMoreFloatView: false
    });
  },
  collentPostClick() {
    this.hiddenFloatView();
    var _this = this;
    var map = {};
    map.contentId = this.data.itemId
    map.type = "5";
    map.userId = app.USER_ID();
    api.getRequestData(app.postShouChangUrl, map, false, "POST").then(res => {
      _this.data.canEvaluate = true;
      if (res.data.errorCode == 0) {
        app.showToasts("收藏帖子成功～");
      } else {
        app.alert("res.data.errorMsg");
      }
    }).catch(err => {
      _this.data.canEvaluate = true;
    });
  },
  juBaoHeClick() {
    this.hiddenFloatView();
    this.setData({
      isShowJuBao: true,
      showJuBao: true
    });
  },
  // 举报信息
  juBaoTextClick(e) {
    var key = e.currentTarget.dataset.key;
    this.setData({
      juBaoType: key,
      isShowJuBao: false
    });
  },
  hiddenMoreView() {
    this.setData({
      showJuBao: false,
      isShowJuBao: true
    });
  },
  juBaoDetailInput(e) {
    this.data.juBaoText = e.detail.value;
  },
  goToChooseImage() {
    var count = 7 - this.data.imageList.length;
    if (count <= 0) {
      app.alert("最多只能传6张图片~");
      return;
    }
    var _this = this;
    app.chooseImageAndUpLoad(count).then(res => {
      if (res) {
        _this.setData({
          imageList: _this.data.imageList.concat(res)
        });
      }
    });
  },
  juBaoClick() {
    // 开始举报
    if (this.data.juBaoText.length == 0) {
      app.alert("请输入举报详情~");
      return;
    }
    var _this = this;
    var map = {};
    map.content = this.data.juBaoText;
    var list = [];
    if (this.data.imageList.length > 1) {
      for (var i = 0; i < this.data.imageList.length; i++) {
        if (i > 0) {
          list.push(this.data.imageList[i]);
        }
      }
    }
    map.imgs = list;
    map.itemId = this.data.itemId
    map.itemType = "5";
    map.type = this.data.juBaoType;
    map.userId = app.USER_ID();
    api.getRequestData(app.juBaoSaveUrl, map, false, "POST").then(res => {
      _this.data.canEvaluate = true;
      if (res.data.errorCode == 0) {
        app.alert("举报提交成功～");
        _this.setData({
          showJuBao: false,
          isShowJuBao: true

        });
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }

    }).catch(err => {
      _this.data.canEvaluate = true;
    });
  },
  goToMesasge(){
    var path = "/pages/message/messageJS/messageJS?fromUserId="+this.data.authorUserId + "&toUserId="+app.USER_ID();
    wx.navigateTo({
      url: path,
    })
  },
  goToVoteDetial(e) {
    var id = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/index/huaTiList/postList?topicId=' + id,
    })
  },
  goToDetail(e){
    var index = e.currentTarget.dataset.key;
    var dict = this.data.dataList[index];
    var userId = dict.userId;
    var id = dict.id;
    var path = '/pages/index/detail/detail?USER_ID=' + userId + '&POST_ID=' + id;
    wx.navigateTo({
      url: path,
    })
  },
})
